package com.itxxs.controller;

import com.itxxs.entity.Order;
import com.itxxs.entity.OrderVO;
import com.itxxs.repository.OrderRepository;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/order")
public class OrderHandler {
    @Autowired
    private OrderRepository orderRepository;
    @Value("${server.port}")
    private String port;
    @RequestMapping(value = "/index")
    public String index(){
        return "当前order端口为:"+this.port;
    }
    @PostMapping("/save")
    public void save(@RequestBody Order order){
        orderRepository.save(order);
    }
    @GetMapping("/findAllByUid/{index}/{limit}/{uid}")
    public OrderVO findAll(@PathVariable("index") int index, @PathVariable("limit") int limit, @PathVariable("uid") long uid){
        List<Order> orderList = orderRepository.findAllByUid(index,limit,uid);
        return new OrderVO(0,"",orderRepository.countByUid(uid),orderList);
    }
    @GetMapping("/countByUid/{uid}")
    public int countByUid(@PathVariable("uid")long uid){
        return orderRepository.countByUid(uid);
    }
    @GetMapping("/findAllByState/{index}/{limit}")
    public OrderVO findAllByState(@PathVariable("index") int index,@PathVariable("limit") int limit){
        List<Order> orderList = orderRepository.findAllByState(index,limit,0);
        return new OrderVO(0,"",orderRepository.count(),orderList);
    }
    @PutMapping("/updateState/{id}")
    public void updateState(@PathVariable("id") long id){
        orderRepository.updateState(id);
    }
}
